/**
 * 发票页面
 * author wim
 */
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_aygsh/base/Constant.dart';
import '../../util/AppSaveInfoUtil.dart';
import 'package:dio/dio.dart';
import '../../http/Api.dart';
import '../../http/HttpUtil.dart';
import '../../util/SignUtil.dart';
import 'dart:convert';
import '../../entity/user_receipt_entity.dart';
import '../Invoice/AddInvoice.dart';
class ChooseReceiptPage extends StatefulWidget {
  ChooseReceiptPage({Key key}) : super(key: key);

  @override
  _ChooseReceiptPageState createState() => _ChooseReceiptPageState();
}

class _ChooseReceiptPageState extends State<ChooseReceiptPage> {
  EasyRefreshController _controller = new EasyRefreshController();
  List<UserReceiptListDataData> dataList = new List();
  int page = 1;
  String token;
  @override
  void initState() {
    getString(TOKEN).then((value) {
      token = value;
      getList();
    });

    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  Future getList() async {
    //获取发票列表
    Map map = new Map.from({
      "sign_type": createSignType(Api.URL_INVOICE_LIST),
      'token': token,
      'p': page.toString(),
    });
    Response<String> response =
        await HttpUtil().post(Api.URL_INVOICE_LIST, data: getSign(map));
       
    Map userMap = json.decode(response.data);
      UserReceiptListEntity homeListEntity =
        new UserReceiptListEntity.fromJson(userMap);
    print(userMap);
    if (userMap['code'] == 1 && mounted) {
      setState(() {
        if (page == 1) {
          dataList.clear();
          _controller.finishRefresh();
          dataList.addAll(homeListEntity.data.datas);
        } else {
          dataList.addAll(homeListEntity.data.datas);
          _controller.finishLoad();
        }
      });
    } else {
      _controller.finishRefresh();
      _controller.finishLoad();
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "选择发票",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0.2,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
        actions: <Widget>[
          GestureDetector(
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.fromLTRB(0, 0, 13, 0),
              child: Text(
                  '取消选择',
                style: TextStyle(
                    fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                    color: Colors.black54),
              ),
            ),
            onTap: () {
               Navigator.of(context).pop();
            },
          )
        ],
      ),
      body: new Column(
        children: <Widget>[
          Container(
            width: double.infinity,
            padding: EdgeInsets.fromLTRB(
                ScreenUtil.getInstance().setWidth(DP_30),
                ScreenUtil.getInstance().setWidth(DP_20),
                ScreenUtil.getInstance().setWidth(DP_30),
                ScreenUtil.getInstance().setWidth(DP_20)),
            color: white,
            child: new GestureDetector(
              child: Container(
                decoration: BoxDecoration(
                    color: Color.fromARGB(255, 245, 245, 245),
                    borderRadius: BorderRadius.circular(80.0)),
                child: TextField(
                  cursorColor: mainColor,
                  maxLines: 1,
                  textAlign: TextAlign.center,
                  keyboardAppearance: Brightness.light,
                  decoration: new InputDecoration(
                      hintText: "订单名称/订单号/手机号",
                      border: InputBorder.none,
                      hintStyle: TextStyle(
                          color: Colors.black45,
                          fontSize: ScreenUtil.getInstance()
                              .setSp(NORMAL_FONT_SIZE))),
                ),
              ),
              onTap: () {
                print("搜索页面跳转");
              },
            ),
          ),
          Expanded(
            child: EasyRefresh(
              enableControlFinishLoad: true,
              controller: _controller,
              enableControlFinishRefresh: true,
              onLoad: () {
                page++;
                getList();
              },
              onRefresh: () {
                page = 1;
                getList();
              },
              child: ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return renderRow(context, index);
                },
                itemCount: dataList.length,
              ),
            ),
          )
        ],
      ),
    );
  }

  renderRow(BuildContext context, int index) {
    return GestureDetector(
      onTap: (){
        UserReceiptListDataData data = dataList[index];
        Navigator.of(context).pop(data);
      },
      child:Container(
      padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
      margin: EdgeInsets.fromLTRB(
          ScreenUtil.getInstance().setHeight(DP_20),
          ScreenUtil.getInstance().setHeight(DP_20),
          ScreenUtil.getInstance().setHeight(DP_20),
          0),
      decoration: BoxDecoration(
          color: white,
          borderRadius:
              BorderRadius.circular(ScreenUtil.getInstance().setHeight(DP_10))),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text("抬头: "+dataList[index].purchaser,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                  height: 1.5,
                  fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                  color: Colors.black87)),
          Container(
              height: dataList[index].is_default == "N" ? 0 : 1,
              margin: EdgeInsets.only(top: 8, bottom: 8),
              color: f0Color),
          Offstage(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Text(''),
                  ),
                  new Container(
                      padding: EdgeInsets.fromLTRB(8, 2, 8, 2),
                      decoration: new BoxDecoration(
                          border: Border.all(color: mainColor, width: 1),
                          borderRadius: BorderRadius.circular(
                              ScreenUtil.getInstance().setHeight(2))),
                      child: new Text(
                        "默认",
                        style: new TextStyle(
                            color: mainColor,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(SMALL_FONT_SIZE - 3)),
                      ))
                ],
              ),
              offstage: dataList[index].is_default == "N")
        ],
      ),
    )) 
    ;
  }
}
